package cn.JHai.rbac.controller;

import cn.JHai.rbac.RequiredPermission;
import cn.JHai.rbac.domain.Role;
import cn.JHai.rbac.query.PageResult;
import cn.JHai.rbac.query.QueryObject;
import cn.JHai.rbac.service.PermissionService;
import cn.JHai.rbac.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    RoleService roleService;

    @Autowired
    PermissionService permissionService;

    /**
     * 显示role，运用了分页
     * @param queryObject
     * @param model
     * @return
     */
    @RequestMapping("/list")
    @RequiredPermission({"获取角色","role:list"})
    public String list(QueryObject queryObject ,Model model){
        PageResult result = roleService.query(queryObject);

        model.addAttribute("result",result);
        return "/role/list";
    }

    /**
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @RequiredPermission({"删除角色","role:delete"})
    public String list(  Model model,Long id){
        roleService.Delete(id);
        return "redirect:/role/list";
    }


    @RequestMapping("input")
    public String input(Model model,Long id){
        if(id!=null){
            model.addAttribute("entity",roleService.get(id));
            Role role = roleService.get(id);
            System.out.println(role.getPermissions());
        }

        model.addAttribute("permissions",permissionService.findAll());
        return "role/input";
    }

    @RequestMapping("saveOrUpdate")
    @RequiredPermission({"更新和添加角色","role:saveOrUpdate"})
    public String saveOrUpdate(Role role,Long ids[]){
        roleService.saveOrUpdata(role,ids);
        return "redirect:/role/list";
    }
}
